<?php
namespace app\api\controller\product;


use app\api\controller\Api;
use app\common\product\model\Product;
use app\common\product\validate\ProductValidate;
use app\common\system\logic\FormLogic;
use think\db\exception\DbException;
use util\Format;

class Goods extends Api
{
    public function index()
    {
        $post = $this->request->post();

        $where = [];
        // 获取field
        if (isset($post['keyword']) && !empty($post['keyword'])) $where[] = ['title', 'like', "%{$post['keyword']}%"];

        $model = new Product();
        list($cfg) = Format::pagination($post);
        $pg = $model->where($where)->paginate($cfg);
        $rows = $pg->getCollection();

        // 返回接口数据
        $result['list'] = $rows;
        $result['pagination'] = Format::pgResult($pg);

        return success($result, '获取数据成功');
    }

    public function read()
    {
        $post = $this->request->post();
        if (empty($post['id'])) return error([], '参数错误');

        $model = new Product();
        $result['row'] = $model->where('id', $post['id'])->find();

        return success($result);
    }

    public function create()
    {
        $post = $this->request->post();
        // 创建的时候清除干扰的信息
        unset($post['id']);
        // 验证提交
        $validate = new ProductValidate();
        if (!$validate->scene('create')->check($post)) return error([], $validate->getError());

        $model = new Product();
        $user = $this->request->user;
        $post['operator_id'] = $user['id'];
        try {
            $model->save($post);
        } catch (DbException $e) {
            return error([], "添加商品分类失败");
        }

        if ($model->id > 0) {
            // 核销表单token
            $formLogic = new FormLogic();
            $formLogic->removeToken($post['form_token']);

            return success([], '添加商品分类成功');
        } else {
            return error([], '添加商品分类失败');
        }
    }

    public function save()
    {
        $post = $this->request->post();

        // 验证提交
        $validate = new ProductValidate();
        if (!$validate->scene('edit')->check($post)) return error([], $validate->getError());

        $model = new Product();
        $row = $model->where('id', $post['id'])->find();
        if (empty($row)) return error([], '商品分类已不存在');

        $user = $this->request->user;
        $post['operator_id'] = $user['id'];
        $rs = $row->save($post);

        if ($rs) {
            // 核销表单token
            $formLogic = new FormLogic();
            $formLogic->removeToken($post['form_token']);

            return success([], '编辑商品分类信息成功');
        } else {
            return error([], '编辑商品分类信息失败');
        }
    }

    public function delete()
    {
        $post = $this->request->post();
        // 验证提交
        $validate = new ProductValidate();
        if (!$validate->scene('delete')->check($post)) return error([], $validate->getError());

        $model = new Product();
        $row = $model->where('id', $post['id'])->find();
        if (empty($row)) return error([], '商品分类已不存在');
        $rs = $row->delete();

        if ($rs) {
            return success([], '删除成功');
        } else {
            return error([], '删除失败');
        }
    }
}